#include <QtTest>
#include <QSignalSpy>

#include "test_SequenceEngine.h"
#include "SequenceModel.h"
#include "sequencerunner.h"
#include "SequenceEngine.h"

#include "mocktimerservice.h"

test_SequenceEngine::test_SequenceEngine()
{
}

void test_SequenceEngine::init()
{
   itemList = new QList<SequenceItem *>();
   model = new SequenceModel( *itemList );
   timerService = new MockTimerService();
   runner = new SequenceRunner( *model, timerService);

   engine = new SequenceEngine ( *runner );
}

void test_SequenceEngine::cleanup()
{
   delete engine;
   delete runner;
   delete timerService;
   delete model;
   delete itemList;
}

/**
 * playing an empty model. Two signals 'paused' occour.
 */
void test_SequenceEngine::playEmptyModel()
{
   QSignalSpy sequenceRunningSpy( engine, SIGNAL(sequenceIsRunning(bool)) );

   runner->selectIndex(0);
   engine->play();

   QCOMPARE( sequenceRunningSpy.length(), 2);
   QCOMPARE( sequenceRunningSpy.at(0).at(0).toBool(), false);
   QCOMPARE( sequenceRunningSpy.at(1).at(0).toBool(), false);
}

/**
 * play an item instantiated but not initialized.
 */
void test_SequenceEngine::playUninitializedEntry()
{
   QSignalSpy sequenceRunningSpy( engine, SIGNAL(sequenceIsRunning(bool)) );
   QSignalSpy sequenceErrorSpy( engine, SIGNAL(sequenceError(QString)) );

   model->createEmptyItem();
   runner->selectIndex(0);
   engine->play();

   QCOMPARE( sequenceRunningSpy.length(), 1);
   QCOMPARE( sequenceRunningSpy.at(0).at(0).toBool(), true);

   QCOMPARE( sequenceErrorSpy.length(), 1);
   QCOMPARE( sequenceErrorSpy.at(0).at(0).toString(), QString("Invalid event type") );
}

void test_SequenceEngine::playValidMedia()
{
   QSignalSpy sequenceRunningSpy( engine, SIGNAL(sequenceIsRunning(bool)) );
   QSignalSpy trackActivateSpy( engine, SIGNAL(activateMedia(QString,bool)) );

   SequenceItem * item = new SequenceItem( -1, "Track 01", SequenceItem::TRACK,
                                           SequenceItem::NEXT_IS_UNDEFINED, this);
   model->addSequenceItem( item);
   runner->selectIndex(0);
   engine->play();

   QCOMPARE( sequenceRunningSpy.length(), 1);
   QCOMPARE( sequenceRunningSpy.at(0).at(0).toBool(), true);

   QCOMPARE( trackActivateSpy.length(), 1);
   QCOMPARE( trackActivateSpy.at(0).at(0).toString(), QString("Track 01") );
   QCOMPARE( trackActivateSpy.at(0).at(1).toBool(), true); /* true: play immediately */
}

void test_SequenceEngine::playValidLight()
{
   QSignalSpy sequenceRunningSpy( engine, SIGNAL(sequenceIsRunning(bool)) );
   QSignalSpy activateLightSpy( engine, SIGNAL(activateLight(QString,bool)) );

   SequenceItem * item = new SequenceItem( -1, "Light set 1", SequenceItem::LIGHT,
                                           SequenceItem::NEXT_IS_UNDEFINED, this);
   model->addSequenceItem( item);
   runner->selectIndex(0);
   engine->play();

   QCOMPARE( sequenceRunningSpy.length(), 1);
   QCOMPARE( sequenceRunningSpy.at(0).at(0).toBool(), true);

   QCOMPARE( activateLightSpy.length(), 1);
   QCOMPARE( activateLightSpy.at(0).at(0).toString(), QString("Light set 1") );
   QCOMPARE( activateLightSpy.at(0).at(1).toBool(), true); /* true: play immediately */
}

